<template>
  <div class="app-container">
    <span style="font-size: medium;color: rgba(151,170,195) "> / 采购调拨</span>

    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="85px" style="margin-top: 20px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
            v-model="queryParams.orderNo"
            placeholder="请输入采购订单编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供货商名称" prop="companyName">
        <el-input
            v-model="queryParams.companyName"
            placeholder="请输入供货商名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <el-option
              v-for="dict in execution_order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="orderExecutionList"
              style="margin-top: 15px">
      <el-table-column label="订单编号" fixed align="center" prop="orderNo" width="125">
        <template #default="scope">
          <router-link :to="'/OrderDetail/index/' + scope.row.orderNo" class="link-type">
            <span>{{ scope.row.orderNo }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="供货商名称" align="center" prop="companyName" width="120"/>
      <el-table-column label="下单时间" align="center" prop="orderTime" width="120">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="responsiblePerson" width="100"/>
      <el-table-column label="修改人" align="center" prop="modifiedBy" width="100"/>
      <el-table-column label="修改日期" align="center" prop="modifiedDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.modifiedDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template #default="scope">
          <el-tag :type="primary" disable-transitions>
            {{ scope.row.orderStatus }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="left" header-align="center" prop="remarks" width="180"/>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width" width="160">
        <template #default="scope">
          <el-button link type="primary" icon="Calendar" @click="showDelivery(scope.row)">调拨计划</el-button>
<!--          <el-button link type="primary" icon="Location" @click="showDeliveryDetail(scope.row)">配送跟踪</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script setup name="PurchaseExecution">
import {parseTime} from "@/utils/ruoyi.js";
import {listPurchaseExecutionOrder} from "@/api/purchase/purchaseOrder.js";
import {getAllocationPlanByOrderNo} from "@/api/purchase/AllocationPlan.js";

const {proxy} = getCurrentInstance();

const open = ref(false);
const loading = ref(true);
const total = ref(0);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  orderNo: "",
  companyName: "",
  orderStatus: "",
});
const allocationPlans = ref([]);
const {execution_order_status} = proxy.useDict("execution_order_status");
const orderExecutionList = ref([]);

function getList() {
  loading.value = true;
  listPurchaseExecutionOrder(queryParams.value).then(response => {
    orderExecutionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 跳转到调拨计划 */
function showDelivery(row){
  proxy.$router.push({
    path:'/AllocationPlan',
    query:{
      orderNo: row.orderNo
    }
  });
}

/** 跳转到配送明细 */
function showDeliveryDetail(row){
  getAllocationPlanByOrderNo(row.orderNo).then(response => {
    allocationPlans.value = response.data;
    if(allocationPlans.value.length > 0){
      setTimeout(() => {
        proxy.$router.push({
          path:'/DeliveryDetail',
          query:{
            allocationNo: allocationPlans.value[0].allocationNo
          }
        });
      },500);
    }
    else{
      proxy.$modal.msgWarning('暂无调拨计划！')
    }
  })
}

getList();
</script>

<style scoped lang="scss">

</style>